﻿
using Dapper;
using HBase;
using Net.http;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Transactions;

namespace hCommand
{
    /**
   *┌────────────────────────────────────────────────┐
   *│　描    述：Component                                                    
   *│　作    者：sgj                                              
   *│　版    本：1.0                                              
   *│　创建时间：2020/9/2 11:28:16                        
   *└────────────────────────────────────────────────┘
   **/
    public  class InLocEngine<CS> : AbsTaskCSEngine where CS : AbsCoreStock {
        public String boxCode;
        public int stn;
        protected CS cs;
        protected override TransRes Execute() {
            List<CoreTask> coreTaskList = getCodeInTask( );
            if( coreTaskList.Count == 1 ) { 
               return continueTask(  coreTaskList[0]); 
            } else if( coreTaskList.Count > 1 ) {
                return new TransRes(true ,boxCode + "at" + stn + "working 不能入库!");
            }
            before( );
            cs = getStock( );
            if(!BUtil.eq(cs.status, StockStatus.库外, StockStatus.盘库库外)) {
                return new TransRes(boxCode + "at" + stn + "状态不能入库!" + cs.status);
            } 
            cs.locId = stn; // 可能原来入库站台，与正式入库口不一直。
            CoreWhLoc loc = selectLoc(  cs);
            if( loc == null ) { 
                CoreClassify area = getById<CoreClassify>(cs.classifyId);
                if (area == null) { 
                    return new TransRes(boxCode +  "物料分类不存在"+ cs.classifyId);
                } else {
                    return new TransRes(boxCode + area.name + "无可用货位");
                } 
            } 
            createInTask(  cs ,( int ) loc.id  ); 
            return afterIntask( cs ,   loc);
        }
        TransRes continueTask(  CoreTask ct) {
            String sql = "select * from Core_task_param where task_id=@taskId  and type={0} and status <= {1}  and status>={2}";

            sql= HUtil.formatInt(sql, TaskParamType.托盘搬运, CoreTaskParamStatus.执行结束, CoreTaskParamStatus.可以接收任务);
            List<CoreTaskParam> list = query<CoreTaskParam>(sql, new { taskId = ct.id });
            if (list.Count > 0) {
                return continueParam(  ct, list);
            } else {
                return mastNewParam(  ct); 
            }
         
        }

        virtual public TransRes continueParam( CoreTask ct, List<CoreTaskParam> list) {
            return new TransRes();
        }
   

        public virtual TransRes mastNewParam(  CoreTask ct) {
            CS cs = getById<CS>(ct.stockId);
            CoreWhLoc loc = selectLoc( cs);
            moveCoreTaskParam(  ct. id, stn   , (int)loc.id );
            if (BUtil.eq(cs.status, StockStatus.库外)) {
                UpdateStock(  cs.id, StockStatus.入库中);
            } else if (BUtil.eq(cs.status, StockStatus.盘库库外, StockStatus.盘库出库)) {
                UpdateStock(  cs.id, StockStatus.盘库回库);
            }

            LocCoreUtil.UpdataWhLoc(this, loc.id, cs.deepDiff);
            return new TransRes();  
        } 
      

       
  
      
        protected virtual TransRes afterIntask(CS cs  ,CoreWhLoc loc) {
            return new TransRes();
        }
        protected virtual void before() {
        }

        protected virtual List<CoreTask> getCodeInTask() {
            string sql = "select * from Core_Task where  status <9 and  status>0 ";

            if( !String.IsNullOrWhiteSpace(boxCode) ) {
                sql += "  and box_Code=@boxCode  ";
            } else {
                sql += "  and src =  " + stn;
            }
            List<CoreTask> coreTaskList = query<CoreTask>(sql ,new {
                boxCode
            }  ) ;
            return coreTaskList;
        }

        public virtual CS getStock() {
            string sql = @"select * from Core_stock where "+ AbsCoreStock .isStoreStr+ "  and box_Code=@boxCode  ";
           
            List<CS> stockList =query<CS>(sql ,new { boxCode } );
            if( stockList.Count == 0 ) {
                String info = boxCode + "没有库位异常stock!";
                throw new Exception(info);
            }
            if( stockList.Count > 1 ) {
                String info = boxCode + "重复的库位异常stock!";
                throw new Exception(info);
            }
            CS cs = stockList[0];
            return cs;
        }

        protected override StockStatus change2Status() {
            if (BUtil.eq(cs.status, StockStatus.库外)) {
                return StockStatus.入库中;
            } else if (BUtil.eq(cs.status, StockStatus.盘库库外)) {
                return StockStatus.盘库回库;

            } else {
                throw new Exception("状态异常");
            }
       
        }
    }
}
